<?php
namespace app\common\model;

use think\Model;


class WxPay Extends Model{
    private $appid = "";
    private $mch_id = "";
    private $device_info = "";
    private $nonce_str = "";
    private $sign = "";
    private $sign_type = "";
    private $body = "";
    private $detail = "";
    private $attach = "";
    private $out_trade_no = "";
    private $fee_type = "CNY";
    private $total_fee = "";
    private $spbill_create_ip = "";
    private $time_start = "";
    private $time_expire = "";
    private $goods_tag = "";
    private $notify_url = "";
    private $trade_type = "";
    private $product_id = "";
    private $limit_pay = "";
    private $openid = "";
    private $scene_info = "";
    private $wxpay_key = "";

    function __construct() {
        $this->appid = Config('wx.mini_appid');
        $this->mch_id = Config('wx.mch_id');
        $this->nonce_str = create_password(32);
        $this->sign_type = "MD5";
        $this->spbill_create_ip = Config('wx.spbill_create_ip');
        $this->wxpay_key = Config('wx.key');
        $this->notify_url = Config('wx.notify_url');
//        $this->spbill_create_ip = get_client_ip(0);
        $this->time_start = time();
    }

    /**
     * @return string
     */
    public function getDeviceInfo() {
        return $this->device_info;
    }

    /**
     * @param string $device_info
     */
    public function setDeviceInfo($device_info) {
        $this->device_info = $device_info;
    }

    public function setAppid($appid) {
        $this->appid = $appid;
    }

    /**
     * @return string
     */
    public function getBody() {
        return $this->body;
    }

    /**
     * @param string $body
     */
    public function setBody($body) {
        $this->body = $body;
    }

    /**
     * @return string
     */
    public function getDetail() {
        return $this->detail;
    }

    /**
     * @param string $detail
     */
    public function setDetail($detail) {
        $this->detail = $detail;
    }

    /**
     * @return string
     */
    public function getAttach() {
        return $this->attach;
    }

    /**
     * @param string $attach
     */
    public function setAttach($attach) {
        $this->attach = $attach;
    }

    /**
     * @return string
     */
    public function getOutTradeNo() {
        return $this->out_trade_no;
    }

    /**
     * @param string $out_trade_no
     */
    public function setOutTradeNo($out_trade_no) {
        $this->out_trade_no = $out_trade_no;
    }

    /**
     * @return string
     */
    public function getFeeType() {
        return $this->fee_type;
    }

    /**
     * @param string $fee_type
     */
    public function setFeeType($fee_type) {
        $this->fee_type = $fee_type;
    }

    /**
     * @return string
     */
    public function getTotalFee() {
        return $this->total_fee;
    }

    /**
     * @param string $total_fee
     */
    public function setTotalFee($total_fee) {
        $this->total_fee = $total_fee;
    }

    /**
     * @return string
     */
    public function getTimeStart() {
        return $this->time_start;
    }

    /**
     * @param string $time_start
     */
    public function setTimeStart($time_start) {
        $this->time_start = $time_start;
    }

    /**
     * @return string
     */
    public function getTimeExpire() {
        return $this->time_expire;
    }

    /**
     * @param string $time_expire
     */
    public function setTimeExpire($time_expire) {
        $this->time_expire = $time_expire;
    }

    /**
     * @return string
     */
    public function getGoodsTag() {
        return $this->goods_tag;
    }

    /**
     * @param string $goods_tag
     */
    public function setGoodsTag($goods_tag) {
        $this->goods_tag = $goods_tag;
    }

    /**
     * @return string
     */
    public function getNotifyUrl() {
        return $this->notify_url;
    }

    /**
     * @param string $notify_url
     */
    public function setNotifyUrl($notify_url) {
        $this->notify_url = $notify_url;
    }

    /**
     * @return string
     */
    public function getTradeType() {
        return $this->trade_type;
    }


    /**
     * @param string $trade_type
     */
    public function setTradeType($trade_type) {
        $this->trade_type = $trade_type;
    }



    /**
     * @return string
     */
    public function getProductId() {
        return $this->product_id;
    }

    /**
     * @param string $product_id
     */
    public function setProductId($product_id) {
        $this->product_id = $product_id;
    }

    /**
     * @return string
     */
    public function getLimitPay() {
        return $this->limit_pay;
    }

    /**
     * @param string $limit_pay
     */
    public function setLimitPay($limit_pay) {
        $this->limit_pay = $limit_pay;
    }

    /**
     * @return string
     */
    public function getOpenid() {
        return $this->openid;
    }

    /**
     * @param string $openid
     */
    public function setOpenid($openid) {
        $this->openid = $openid;
    }

    /**
     * @return string
     */
    public function getSceneInfo() {
        return $this->scene_info;
    }

    /**
     * @param string $scene_info
     */
    public function setSceneInfo($scene_info) {
        $this->scene_info = $scene_info;
    }

    public function setWxpayKey($wxpay_key) {
        $this->wxpay_key = $wxpay_key;
    }

    public function setMchId($mch_id) {
        $this->mch_id = $mch_id;
    }

    private function getParam() {
        $data = array(
            'appid' => $this->appid,
            'mch_id' => $this->mch_id,
            'device_info' => $this->device_info,
            'nonce_str' => $this->nonce_str,
            'sign' => $this->sign,
            'sign_type' => $this->sign_type,
            'body' => $this->body,
            'detail' => $this->detail,
            'attach' => $this->attach,
            'out_trade_no' => $this->out_trade_no,
            'fee_type' => $this->fee_type,
            'total_fee' => $this->total_fee,
            'spbill_create_ip' => $this->spbill_create_ip,
            'time_start' => $this->time_start,
            'time_expire' => $this->time_expire,
            'goods_tag' => $this->goods_tag,
            'notify_url' => $this->notify_url,
            'trade_type' => $this->trade_type,
            'product_id' => $this->product_id,
            'limit_pay' => $this->limit_pay,
            'openid' => $this->openid,
            'scene_info' => $this->scene_info,
        );
        $data = array_filter($data);
        return $data;
    }

    private function createSign($array) {
        ksort($array);
        $data = http_build_query($array);
        $data = urldecode($data);
        $data .= '&key=' . $this->wxpay_key;
        $sign = strtoupper(md5($data));
        return $sign;
    }

    public function ready() {
        $this->sign = $this->createSign($this->getParam());
       // $data = json_decode($this->attach,true);
        $xml = arrayToXml($this->getParam(), 'xml');
        $result = curl_post(Config('wx.pay_url'), $xml);
        return xmlToArray($result);
    }

    /**
     * 校验微信返回的正确性
     * @param array $data 微信回传的数组
     * @return bool  返回正确性
     */
    function checkSign($data = array()) {
        $sign = $data['sign'];
        $data['sign'] = null;
        ksort($data);
        $data = array_filter($data);
        $data = http_build_query($data);
        $data = urldecode($data);
        $data .= "&key=" . $this->wxpay_key;
        $mySign = strtoupper(md5($data));


        if ($mySign == $sign) {
            return true;
        } else {
            return false;
        }
    }


     function getPaySign($package = "") {
        $data = array(
            'appId' => $this->appid,
            'timeStamp' => time(),
            'nonceStr' => create_password(32),
            'package' => 'prepay_id=' . $package,
            'signType' => 'MD5'
        );
        ksort($data);
        $wSign = array_filter($data);
        $wSign = http_build_query($wSign);
        $wSign = urldecode($wSign);
        $wSign .= "&key=" . $this->wxpay_key;
        $mySign = strtoupper(md5($wSign));
        $data['paySign'] = $mySign;
        return $data;
    }

}